import { mapGetters, mapActions } from 'vuex'
import { themeList, addCss, removeAllCss } from './book'
import { saveLocation, saveReadTime, getReadTime, getBookmark,getBookShelf,saveBookShelf } from '../utils/localStorage'
// import { shelf } from '../api/store'
const storeHomeMixin={
    computed: {
        ...mapGetters([
          'offsetY',
          'hotSearchOffsetY',
          'flapCardVisible'
        ])
      },
      methods: {
        ...mapActions([
          'setOffsetY',
          'setHotSearchOffsetY',
          'setFlapCardVisible'
        ]),
        showBookDetail(book) {
         this.$router.push({
             path:'/store/detail',
             query:{
                 fileName:book.fileName,
                 category:book.categoryText
             }
         })
        }
      }
}
const ebookMixin = {
    computed: {
        ...mapGetters(["fileName",//接收到的参数值
            "menuVisible",//菜单和标题的显示或者隐藏
            'settingVisible',
            'defaultPoint',
            'currentBook',
            'defaultFontFamily',
            'fontFamilyVisible',
            'defaultTheme',
            'bookAvailable',
            'progress',
            'section',
            'isPaginating',
            'navigation',
            'cover',
            'metadata',
            'paginate',
            'pagelist',
            'offsetY',
            'isBookmark']),

        themeList() {
            return themeList(this);
        },
        getSectionName() {
     
            return this.section ? this.navigation[this.section].label : "";
          },
    }, //？？？？？？？？？？？？？,
    methods: {
        ...mapActions(["setMenuVisible",
            "setFileName",
            'setSettingVisible',
            'setDefaultPoint',
            'setCurrentBook',
            'setDefaultFontFamily',
            'setFontFamilyVisible',
            'setDefaultTheme',
            'setBookAvailable',
            'setProgress',
            'setSection',
            'setIsPaginating',
            'setNavigation',
            'setCover',
            'setMetadata',
            'setPaginate',
            'setPagelist',
            'setOffsetY',
            'setIsBookmark']),    //引入vuex actions 简化
        initGlobalCss() {
            removeAllCss()
            switch (this.defaultTheme) {
                case "Default":
                    addCss("http://localhost:808/theme/theme_default.css");
                    break;
                case "Eye":
                    addCss("http://localhost:808/theme/theme_eye.css");
                    break;
                case "Gold":
                    addCss("http://localhost:808/theme/theme_gold.css");
                    break;
                case "Night":
                    addCss("http://localhost:808/theme/theme_night.css");
                    break;
            }
        },
        //文本
        gettext() {
            return this.$t("book.haveRead").replace("$1", this.replaceTime());
        },
        replaceTime() {
            const timer = getReadTime(this.fileName);
            if (!timer) {
                return 0;
            } else {
                return Math.ceil(timer / 60);
            }
        },
        //刷新当前位置
        refreshLocation() {
            const currentLoction = this.currentBook.rendition.currentLocation();//获取当前页开始的位置
            if (currentLoction && currentLoction.start) {
                const startCfi = currentLoction.start.cfi;
                const progress = this.currentBook.locations.percentageFromCfi(startCfi);
                this.setSection(currentLoction.start.index)
                this.setProgress(Math.floor(progress * 100));
                saveLocation(this.fileName, startCfi);
                const bookmark=getBookmark(this.fileName)
                console.log(bookmark);
               if(bookmark){
                if(bookmark.some(item=>item.cfi===startCfi)){
                    this.setIsBookmark(true)

                }else{
                    this.setIsBookmark(false)
                }
               }else{
                   this.setIsBookmark(false)
               }
            }
        },
        //页面一加载就跳转到location位置
        display(target, cb) {
            if (target) {
                this.currentBook.rendition.display(target).then(() => {
                    this.refreshLocation()
                    if (cb) cb()
                });
            } else {
                this.currentBook.rendition.display().then(() => {
                    this.refreshLocation()
                    if (cb) cb()
                });
            }

        },
        hideTitleAndMenu() {
            // this.$store.dispatch("setMenuVisible", false);
            this.setMenuVisible(false);
            this.setSettingVisible(-1);
            this.setFontFamilyVisible(false); //字体的隐藏显示
        },
    },

}
export const storeShelfMixin = {
    computed: {
      ...mapGetters([
        'isEditMode',
        'shelfList',
        'shelfSelected',
        'shelfTitleVisible',
        'offsetY',
        'shelfCategory',
        'currentType'
      ])
    },
    methods: {
      ...mapActions([
        'setIsEditMode',
        'setShelfList',
        'setShelfSelected',
        'setShelfTitleVisible',
        'setOffsetY',
        'setShelfCategory',
        'setCurrentType'
      ]),
      showBookDetail(book) {
        gotoBookDetail(this, book)
      },
      getCategoryList(title) {
        this.getShelfList().then(() => {
          const categoryList = this.shelfList.filter(book => book.type === 2 && book.title === title)[0]
          this.setShelfCategory(categoryList)
        })
      },
      getShelfList() {
        let shelfList = getBookShelf()
        if (!shelfList) {
          shelf().then(response => {
            if (response.status === 200 && response.data && response.data.bookList) {
              shelfList = appendAddToShelf(response.data.bookList)
              saveBookShelf(shelfList)
              return this.setShelfList(shelfList)
            }
          })
        } else {
          return this.setShelfList(shelfList)
        }
      },
      moveOutOfGroup(f) {
        this.setShelfList(this.shelfList.map(book => {
          if (book.type === 2 && book.itemList) {
            book.itemList = book.itemList.filter(subBook => !subBook.selected)
          }
          return book
        })).then(() => {
          const list = computeId(appendAddToShelf([].concat(
            removeAddFromShelf(this.shelfList), ...this.shelfSelected)))
          this.setShelfList(list).then(() => {
            this.simpleToast(this.$t('shelf.moveBookOutSuccess'))
            if (f) f()
          })
        })
      }
    }
  }
export {
    ebookMixin,
    storeHomeMixin
}